//require Model
const _ = require('lodash');
const mongoose = require('mongoose');
require('../model/sysuser.model');
var sysuserModel = mongoose.model('Sysuser');

module.exports = {
    addSysUser : addSysUser,
    getSysUser : getSysUser,
    login : login
}

function addSysUser(name, password, callback) {
    if(_.isEmpty(name) || _.isEmpty(password)){
       callback(new Error("name or password is empty."), null); 
    }else{
        sysuserModel.create({"name":name, "password":password}, function(err, doc){
            callback(err, doc);
        });
    }
}

function getSysUser(name, password, callback) {
    if(_.isEmpty(name) || _.isEmpty(password)){
       callback(new Error("name or password is empty."), null); 
    }else{
        sysuserModel.findOne({"name":name, "password":password}, function(err, docs){
            callback(err, docs);
        });
    }
}

function login(name, password, callback) {
    getSysUser(name, password, function(err, result){
        if(result){
            callback(err, true);
        }else{
            callback(err, false);
        }
    });
}